﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.ComponentModel;

namespace Asol.Reporting.Editor.Components
{
    public class PanelTitle : DblGraphPanel
    {
        public PanelTitle()
        {
            this.ParentChanged += new EventHandler(_PanelTitle_ParentChanged);
        }
        #region Hlídání změny parenta, reakce na focus v parentu
        void _PanelTitle_ParentChanged(object sender, EventArgs e)
        {
            if (this._LastParent != null)
            {
                this._LastParent.Enter -= new EventHandler(_Parent_Enter);
                this._LastParent.Leave -= new EventHandler(_Parent_Leave);
            }

            this._LastParent = this.Parent;
            if (this._LastParent != null)
            {
                this._LastParent.Enter += new EventHandler(_Parent_Enter);
                this._LastParent.Leave += new EventHandler(_Parent_Leave);
            }
        }

        void _Parent_Enter(object sender, EventArgs e)
        {
            this._Entered = true;
            this.Draw();
        }
        void _Parent_Leave(object sender, EventArgs e)
        {
            this._Entered = false;
            this.Draw();
        }
        private Control _LastParent = null;
        private bool _Entered = false;
        #endregion
        #region Kreslení
        protected override void OnPaintToBuffer(object sender, PaintEventArgs e)
        {
            base.OnPaintToBuffer(sender, e);
            this.PaintBackground(e);
            this.PaintCaption(e);
        }
        private void PaintBackground(PaintEventArgs e)
        {
            Color colorTop = (this._Entered ? this.ColorTopActive : this.ColorTopInactive);
            Rectangle inner = new Rectangle(new Point(0, 0), this.Size);
            int x1 = inner.X;
            int x2 = inner.X + 2;
            int x3 = inner.Right - 3;
            int x4 = inner.Right - 1;
            int y1 = inner.Y;
            int y2 = inner.Y + 2;
            int y3 = inner.Bottom - 1;
            GraphicsPath path = new GraphicsPath();
            path.AddLine(x1, y3, x1, y2);

            // path.AddPie(x1, y1, 4, 4, 180f, 90f);
            path.AddLine(x1, y2, x2, y1);

            path.AddLine(x2, y1, x3, y1);

            // path.AddPie(x4 - 4, y1, 4, 4, 270f, 90f);
            path.AddLine(x3, y1, x4, y2);

            path.AddLine(x4, y2, x4, y3);
            path.AddLine(x4, y3, x1, y3);
            path.CloseFigure();
            using (LinearGradientBrush lgb = new LinearGradientBrush(inner, colorTop, this.BackColor, 90f))
            {
                var state = e.Graphics.Save();
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                e.Graphics.FillPath(lgb, path);
                // e.Graphics.FillRectangle(lgb, inner);
                e.Graphics.Restore(state);
            }
        }
        private void PaintCaption(PaintEventArgs e)
        {
            Rectangle textArea = new Rectangle(16, 3, this.Width - 19, this.Height - 6);
            Color colorText = (this._Entered ? this.ColorTextActive : this.ColorTextInactive);
            using (SolidBrush brush = new SolidBrush(colorText))
            {
                e.Graphics.DrawString(this.Caption, this.CaptionFont, brush, textArea);
            }
        }
        #endregion
        #region Public property a metody
        /// <summary>
        /// Barva horní části panelu, pokud v parent containeru JE focus
        /// </summary>
        [Category("PanelTitle")]
        [Description("Barva horní části panelu, pokud v parent containeru JE focus")]
        public Color ColorTopActive
        {
            get { return this._ColorTopActive; }
            set { this._ColorTopActive = value; this.Draw(); }
        }
        private Color _ColorTopActive = SystemColors.GradientActiveCaption;
        /// <summary>
        /// Barva horní části panelu, pokud v parent containeru NENÍ focus
        /// </summary>
        [Category("PanelTitle")]
        [Description("Barva horní části panelu, pokud v parent containeru NENÍ focus")]
        public Color ColorTopInactive
        {
            get { return this._ColorTopInactive; }
            set { this._ColorTopInactive = value; this.Draw(); }
        }
        private Color _ColorTopInactive = SystemColors.GradientInactiveCaption;
        /// <summary>
        /// Barva textu Caption, pokud v parent containeru JE focus
        /// </summary>
        [Category("PanelTitle")]
        [Description("Barva textu Caption, pokud v parent containeru JE focus")]
        public Color ColorTextActive
        {
            get { return this._ColorTextActive; }
            set { this._ColorTextActive = value; this.Draw(); }
        }
        private Color _ColorTextActive = SystemColors.ActiveCaptionText;
        /// <summary>
        /// Barva textu Caption, pokud v parent containeru NENÍ focus
        /// </summary>
        [Category("PanelTitle")]
        [Description("Barva textu Caption, pokud v parent containeru NENÍ focus")]
        public Color ColorTextInactive
        {
            get { return this._ColorTextInactive; }
            set { this._ColorTextInactive = value; this.Draw(); }
        }
        private Color _ColorTextInactive = SystemColors.InactiveCaptionText;
        /// <summary>
        /// Text, reprezentující titulek
        /// </summary>
        [Category("PanelTitle")]
        [Description("Text, reprezentující titulek")]
        public string Caption
        {
            get { return this._Caption; }
            set { this._Caption = value; this.Draw(); }
        }
        private string _Caption;
        /// <summary>
        /// Písmo pro titulek
        /// </summary>
        [Category("PanelTitle")]
        [Description("Písmo pro titulek")]
        public Font CaptionFont
        {
            get { return this._CaptionFont; }
            set { this._CaptionFont = value; this.Draw(); }
        }
        private Font _CaptionFont = SystemFonts.CaptionFont;
        #endregion
    }
}
